import { _decorator, Component, find, Node, v3, Vec3 } from 'cc';
import { BagManager } from '../Manager/BagManager';
import { DataMgr } from '../../../FrameWork3.8.6/FrameWork/Data/DataMgr';
import { EquipmentDtMgr, PlayerDtMgr, PlayerSkillDtMgr, ShiBingDtMgr } from '../../../FrameWork3.8.6/FrameWork/Data/DataBaseMgr';
import { EquipmentDt, PlayerDt, PlayerSkillDt, ShiBingDt } from '../../../FrameWork3.8.6/FrameWork/Common/CommonInterface';
import { Emitter } from '../../../FrameWork3.8.6/FrameWork/Common/Emitter';
import { UIManager } from '../../../FrameWork3.8.6/FrameWork/UIFramework/UIManager';
import { CharacterBase } from '../Base/CharacterBase';
import { FollowCamera } from './FollowCamera';
const { ccclass, property } = _decorator;

@ccclass('Player')
export class Player extends CharacterBase {

    //玩家自己的信息
    private _playerDt:PlayerDt=null;
    //携带的装备信息
    public  ownEquipmentDt:EquipmentDt[]=null;

    //携带的技能信息
    public ownSkillDt:PlayerSkillDt[]=null;

    //携带兵种信息
    public ownShiBingDt:ShiBingDt[]=null;

    //玩家移动方向
    private _isLeft:boolean=false;
    private _isRight:boolean=false;

    
    
    
    lateInit(...rest:any[]){
        //获取Camera脚本
        let camera=find("Canvas/FollowCamera").getComponent(FollowCamera);
        //这里通过发送消息给相机
        Emitter.instance.emit("FollowCamera",camera);
        this._playerDt=rest[0] as PlayerDt;
        
        //记录当前数据
        this.ownEquipmentDt=BagManager.instance.getBagDt<EquipmentDt>("Equipment",true);
        this.ownSkillDt=BagManager.instance.getBagDt<PlayerSkillDt>("Skill",true);
        this.ownShiBingDt=BagManager.instance.getBagDt<ShiBingDt>("ShiBing",true);

        //玩家数据更新完之后发送消息给mainUI去生成技能按钮，兵种按钮，以及将这些信息全部赋值给mainUI
        Emitter.instance.emit("ConfigInfo",UIManager.instance.getUI("MainUI"),this.ownSkillDt,this.ownShiBingDt);
    }

    normalWalk(dirName:string){
        this.walk();
        if(dirName=="left"){
            //播放向左移动动画
            this._isLeft=true;
        }
        else{
            //播放向右移动动画
            this._isRight=true;
        }
    }

    normalTp(dirName:string){
        //播放TP动画
        if(dirName=="left"){
            this.node.setPosition(v3(this.node.position.x-400,this.node.position.y));
        }
        else{
             this.node.setPosition(v3(this.node.position.x+400,this.node.position.y));
        }
    }

    endMove(dirName:string){
        //播放静止动画

        if(dirName=="left"){
            this._isLeft=false;
        }
        else{
            this._isRight=false;
        }
    }


    onUpdate(dt:number){
        //判断攻击范围是否有敌人
        this.searchEnemy();
        if(this._arrEnemy.length){
            let target=this.getNearestCamp();
            if(!target){
                return;
            }
            this.attack(target);
        }
        let moveX=0
        if(this._isLeft){
            moveX-=100*dt;
        }
        else if(this._isRight){
            moveX+=100*dt;
        }
        this.node.setPosition(v3(this.node.position.x+moveX,this.node.position.y));
        if(this.node.position.x<=-900){
            this.node.setPosition(v3(-900,this.node.position.y))
        }
        if(this.node.position.x>=2820){
            this.node.setPosition(v3(2820,this.node.position.y))
        }
        

        
       
    }

    
}


